package cpr;

import java.io.BufferedReader;
import java.io.InputStreamReader;

import cpr.context.CommandContext;

public class UserInputThread extends Thread {
	private CommandContext ctx;
	public UserInputThread(CommandContext ctx) {
		this.ctx = ctx;
	}

	@Override
	public void run() {
		try {
			boolean exit = false;
			String line=null;
            BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
            Printer.print(ctx.getConsolePrefix());
            while ((!exit)&&((line = br.readLine()) != null)) {
                if (!"".equals(line.trim())) {
                    ctx.pushCommand(ctx.getConsolePrefix(),line);
                    if ("exit".equals(line.trim())) {
                        exit = true;
                    }
                }
                Thread.sleep(10);
                Thread.yield();
                Printer.print(ctx.getConsolePrefix());
            }

		}catch (Exception ex) {
			throw new RuntimeException(ex);
		}
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}
